<template>
    <view class="login-container">
        <uni-icons type="contact-filled" size="100" style="color: #afafaf;"></uni-icons>
        <button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
        <text class="tips-text">登录后尽享更多权益</text>
        <!-- 开发模式按钮 -->
        <button type="default" class="btn-dev" @click="mockLogin">登录不了就点击开发模式吧</button>
    </view>
</template>

<script>
    import {
        mapMutations,
        mapState
    } from 'vuex'

    export default {
        name: "my-login",
        data() {
            return {}
        },
        methods: {
            ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
            getUserProfile() {
                wx.getUserProfile({
                    desc: '用于完善会员资料',
                    success: (res) => {
                        this.updateUserInfo(res.userInfo)
                        this.getToken(res)
                    },
                    fail: () => {
                        uni.$showMsg('您取消了登录授权！')
                    }
                })
            },
            async getToken(info) {
                try {
                    const loginRes = await uni.login()
                    if (loginRes.errMsg !== 'login:ok') {
                        return uni.$showMsg('登录失败！')
                    }

                    const query = {
                        code: loginRes.code,
                        encryptedData: info.encryptedData,
                        iv: info.iv,
                        rawData: JSON.stringify(info.rawData),
                        signature: info.signature
                    }

                    const {
                        data: loginResult
                    } = await uni.$http.post('https://www.uvshop.cn/api/public/v1/users/wxlogin', query)
                    if (loginResult.meta.status !== 200) {
                        return uni.$showMsg('登录失败！')

                    }


                    this.updateToken(loginResult.message.token)

                    this.navigateBack()
                } catch (err) {
                    uni.$showMsg('登录失败！')
                }
            },
            // 开发模式登录，直接设置模拟用户信息和token，并跳转到用户信息页面
            mockLogin() {
                // 模拟用户信息
                const mockUserInfo = {
                    nickName: '测试用户',
                    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132',
                    gender: 0,
                    province: '北京市',
                    city: '北京市',
                    country: '中国',
                    language: 'zh_CN'
                    }
                // 使用带Bearer前缀的模拟token
                const mockToken = 'Bearer mock_token_for_development'

                // 更新用户信息和token
                this.updateUserInfo(mockUserInfo)
                this.updateToken(mockToken)

                // 显示登录成功提示
                uni.$showMsg('开发模式登录成功')
                
                // 调用导航方法处理重定向
                this.navigateBack()
            },
            navigateBack() {
                // 确保正确获取重定向信息
                const redirectInfo = this.$store.state.m_user.redirectInfo;
                if (redirectInfo && redirectInfo.openType === 'switchTab') {
                    uni.switchTab({
                        url: redirectInfo.from,
                        complete: () => {
                            // 清除重定向信息
                            this.updateRedirectInfo(null)
                        }
                    })
                }
            }
        },
        computed: {
            ...mapState('m_user', ['redirectInfo'])

        }
    }
</script>

<style lang="scss">
    .login-container {
        height: 750rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        background-color: #f8f8f8;
        position: relative;
        overflow: hidden;

        &::after {
            content: ' ';
            display: block;
            position: absolute;
            width: 100%;
            height: 40px;
            left: 0;
            bottom: 0;
            background-color: white;
            border-radius: 100%;
            transform: translateY(50%);
        }

        .btn-login {
            width: 90%;
            border-radius: 100px;
            margin: 15px 0;
            background-color: #c00000;
        }

        .tips-text {
            font-size: 12px;
            color: gray;
        }

        .btn-dev {
            width: 90%;
            border-radius: 100px;
            margin-top: 15px;
            font-size: 12px;
        }
    }
</style>